package com.example.month2;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button1,button2,button3,button4,button5,button6,button7,button8,button11,button12;
TextView show;

    private MyDialog selfDialog;
    private MyDataDialog dataDialog;
    String[] items = new String[] {
            "文书", "班长",
            "纪委","学委","安委",
            "生委" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button5 = findViewById(R.id.button5);
        button6 = findViewById(R.id.button6);
        button7 = findViewById(R.id.button7);
        button8 = findViewById(R.id.button8);
        button11 = findViewById(R.id.button11);
        button12 = findViewById(R.id.button12);
        show=findViewById(R.id.show);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button12.setOnClickListener(this);
        button11.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                //普通对话框
                showAlertDialog();
                break;
            case R.id.button2:
                //时间对话框
                showTimeDialog();
                break;
            case R.id.button3:
                //复选对话框
                showMuliteDialog();
                break;
            case R.id.button4:
                //单选对话框
                showSingleDialog();
                break;
            case R.id.button5:
                //进度对话框
                showProgessDialog();
                break;
            case R.id.button6:
                //列表对话框
                showListDialog();
                break;
            case R.id.button7:
                //自定义
                showMyDialog2();
//                showViewDialog();
                break;
            case R.id.button8:
                showMyDialog();
                break;
            case R.id.button11:
                Intent intent =new Intent(MainActivity.this,DialogActivity.class);
                startActivity(intent);
                break;
            case R.id.button12:
                //日期对话框
                showDataDialog();
                break;
        }
    }
    private void showMyDialog2(){
        dataDialog = new MyDataDialog(MainActivity.this);
        dataDialog.show();
    }
    private void showMyDialog(){
        selfDialog = new MyDialog(MainActivity.this);
        selfDialog.setTitle("提示");
        selfDialog.setMessage("确定退出应用?");
        selfDialog.setYesOnclickListener("确定", new MyDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                Toast.makeText(MainActivity.this,"点击了--确定--按钮",Toast.LENGTH_LONG).show();
                selfDialog.dismiss();
            }
        });
        selfDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                Toast.makeText(MainActivity.this,"点击了--取消--按钮",Toast.LENGTH_LONG).show();
                selfDialog.dismiss();
            }
        });

        selfDialog.show();



        Window dialogWindow = selfDialog.getWindow();
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//        Log.e("####height", lp.height+"");
//        Log.e("####width", lp.width+"");

        lp.height = 600;
        lp.width = 300;
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setAttributes(lp);

    }
    private void showAlertDialog() {
        Log.e("#####","showAlertDialog");
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                // 设置对话框标题
                .setTitle("简单对话框")
                // 设置图标
                .setIcon(R.mipmap.tools)
                .setMessage("对话框的测试内容\n第二行内容")
                .setCancelable(true);

        // 为AlertDialog.Builder添加“取消”按钮
        builder.setNegativeButton("消极", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                show.setText("单击了【取消】按钮！");
//                Toast.makeText(this, "单击了【取消】按钮！", Toast.LENGTH_LONG);
            }
        });
        builder.setNeutralButton("中性", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                show.setText("单击了【功能】按钮！");
            }
        });
        // 为AlertDialog.Builder添加“确定”按钮
        builder.setPositiveButton("积极",new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
               show.setText("单击了【确定】按钮！");
//                Toast.makeText(this, "单击了【确定】按钮！", Toast.LENGTH_LONG);
            }
        }) ;

        AlertDialog dialog =  builder.create();
        dialog .show();
    }
    private void showDataDialog() {
        Calendar calendar = Calendar.getInstance();//获得当前日期
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

            }
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
    }
    private void showTimeDialog() {
        Calendar calendar = Calendar.getInstance();//获得当前时间
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            }
        },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
    }
    private void showMuliteDialog() {
        Log.e("#####","showMuliteDialog");
        final boolean[] aa = new boolean[]{false, true, false, true, true, true};
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                // 设置对话框标题
                .setTitle("你想成为哪个班委？")
                // 设置图标
                .setIcon(R.mipmap.tools)
                // 设置多选列表项，设置勾选第2项、第4项
                .setMultiChoiceItems(items
                        , aa, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                aa[which]=isChecked;
                            }
                        });
        // 为AlertDialog.Builder添加“确定”按钮
        setPositiveButton(builder);
        // 为AlertDialog.Builder添加“取消”按钮
        setNegativeButton(builder)
                .create()
                .show();
    }
    private void showSingleDialog() {
        Log.e("#####","showSingleDialog");
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                // 设置对话框标题
                .setTitle("你想当啥？")
                // 设置图标
                .setIcon(R.mipmap.tools)
                // 设置单选列表项，默认选中第二项（索引为1）
                .setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        show.setText("你想当" + items[which] + "？");
                    }
                });
        // 为AlertDialog.Builder添加“确定”按钮
        setPositiveButton(builder);
        // 为AlertDialog.Builder添加“取消”按钮
        setNegativeButton(builder)
                .create()
                .show();
    }
    int currentProgress = 0;
    private void showProgessDialog() {
        currentProgress=0;
        Log.e("#####","showProgessDialog");
        final ProgressDialog pDialog =new ProgressDialog(this);
        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pDialog.setMax(100);
        pDialog.setProgress(0);
        pDialog.setMessage("加载中&……");
        pDialog.show();
        /**
         * 计时器
         */
        new CountDownTimer(11000,1000) {
            @Override
            public void onTick(long millisUntilFinished) { //总-1000
                pDialog.setProgress(currentProgress+=10);
                //改进度值
            }
            @Override
            public void onFinish() {
                //停止时候要做的事情
                pDialog.setMessage("已完成");
                pDialog.dismiss();
            }
        }.start();


    }
    private void showListDialog() {
        Log.e("#####","showCustomDialog");
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                // 设置对话框标题
                .setTitle("自定义列表项对话框")
                // 设置图标
                .setIcon(R.mipmap.tools)
                // 设置自定义列表项
                .setAdapter(new ArrayAdapter<String>(this
                        , android.R.layout.simple_list_item_1
                        , items), null);
        // 为AlertDialog.Builder添加“确定”按钮
        setPositiveButton(builder);
        // 为AlertDialog.Builder添加“取消”按钮
        setNegativeButton(builder)
                .create()
                .show();
    }
/**
 * 思考题如何定义一个登录框
 */

    private void showViewDialog(){
        // 装载app\src\main\res\layout\login.xml界面布局文件
        TableLayout loginForm = (TableLayout)getLayoutInflater().inflate( R.layout.login, null);
        final EditText name = loginForm.findViewById(R.id.name);
        final EditText pass = loginForm.findViewById(R.id.pass);
        EditText phone = loginForm.findViewById(R.id.phone);
        new AlertDialog.Builder(this)
                // 设置对话框的图标
                .setIcon(R.mipmap.tools)
                // 设置对话框的标题
                .setTitle("自定义View对话框")
                // 设置对话框显示的View对象
                .setView(loginForm)
                // 为对话框设置一个“确定”按钮
                .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which) {
                        // 此处可执行登录处理
                        Log.e(name.getText().toString(),pass.getText().toString());
                    }
                })
                // 为对话框设置一个“取消”按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which)
                    {
                        // 取消登录，不做任何事情
                    }
                })
                // 创建并显示对话框
                .create()
                .show();
    }



    /**
     * 确定
     * @param builder
     * @return
     */
    private AlertDialog.Builder setPositiveButton(
            AlertDialog.Builder builder)
    {
        // 调用setPositiveButton方法添加“确定”按钮
        return builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                show.setText("单击了【确定】按钮！");
            }
        });
    }

    /**
     * 取消
     * @param builder
     * @return
     */
    private AlertDialog.Builder setNegativeButton(
            AlertDialog.Builder builder)
    {
        // 调用setNegativeButton方法添加“取消”按钮
        return builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                show.setText("单击了【取消】按钮！");
            }
        });
    }
}
